package com.xiaoyao.login.api;

import com.xiaoyao.common.rest.BaseApi;
import com.xiaoyao.login.pojo.School;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MainApi extends BaseApi {

    @RequestMapping("/main")
    public String main(){
        return "Hello Spring Security";
    }

    @RequestMapping("/hello")
    public String login(){
        return "hello";
    }

    /**
     *@author xiuyao
     *@date 2022/3/18 9:51
     *@description TODO
     *@return java.lang.String
     */
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping("/info")
    public String info(){
        return getPrincipal().getUsername();
    }

    @RequestMapping("/postFilter")
    @PreAuthorize("hasRole('ADMIN')")
    @PostFilter("filterObject.id%2==0")
    public List<School> postFilter() {
        List<School> schools = new ArrayList<School>();
        School school;
        for (int i = 0; i < 10; i++) {
            school = new School();
            school.setId((long)i);
            schools.add(school);
        }
        return schools;
    }
}
